/**
 * 
 */
package com.cater.sys.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cater.common.BaseEntity;
import com.cater.common.PageResult;
import com.cater.query.CommonQuery;
import com.cater.sys.dao.CompanyDao;
import com.cater.sys.dao.DictionarydataDao;
import com.cater.sys.entity.Company;
import com.cater.sys.entity.Dictionarydata;
import com.cater.sys.service.BaseService;
import com.cater.talents.entity.Contact;

/**
 * @Description: TODO
 * @author: yong
 * @time:2018年3月25日 上午12:28:47
 */
@Service
public class CompanyServiceImpl implements BaseService<Company, CommonQuery, String> {
	@Autowired
	CompanyDao dao;
	@Autowired
	DictionarydataDao dictDao;

	@Override
	public PageResult<Company> findPageResult(CommonQuery query) {
		PageResult<Company> result = new PageResult<Company>();
		Dictionarydata dictnature = null;
		Dictionarydata dictscale = null;
		Dictionarydata dictstatus = null;
		result.setPage(query.getPage());
		result.setPageSize(query.getRows());
		int count = dao.findCount(query);
		int first = (query.getPage() - 1) * query.getRows();
		int max = query.getRows();
		List<Company> list = dao.findList(query, first, max);
		for (Company company : list) {
			String nature = company.getNature();
			String scale = company.getScale();
			Integer status = company.getIsStatus();
			if (StringUtils.isNotEmpty(nature)) {
				dictnature = dictDao.getByTypeValue(nature, "company_nature");
				company.setDictNature(dictnature.getValueName());
			} else {
				company.setDictNature("暂无");
			}
			if (StringUtils.isNotEmpty(scale)) {
				dictscale = dictDao.getByTypeValue(scale, "company_scale");
				if (dictscale != null) {
					company.setDictScale(dictscale.getValueName());
				} else {
					company.setDictScale("暂无");
				}
			}
			if (status != null) {
				dictstatus = dictDao.getByTypeValue(status.toString(), "company_status");
				if (dictstatus != null) {
					company.setDictStatus(dictstatus.getValueName());
				}
			} else {
				company.setDictStatus("暂无");
			}

		}
		result.setRows(list);
		result.setRecords(count);
		return result;
	}

	@Override
	public List<Company> getAll() {
		return dao.getAll();
	}

	@Override
	public List<Company> getAllCommon(CommonQuery query) {
		return dao.getAllCommon(query);
	}

	@Override
	public int save(Company t) {
		return 0;
	}

	@Override
	public String saves(Company t) {
		BaseEntity.getCreate(t);
		dao.save(t);
		return t.getId();
	}

	@Override
	public void update(Company t) {
		BaseEntity.getupdate(t);
		dao.update(t);
	}

	@Override
	public void delete(String id) {
		dao.delete(id);
	}

	@Override
	public Company getById(String id) {
		return dao.getById(id);
	}

	public Company getByName(CommonQuery query) {
		return dao.getByName(query);
	}

	public List<Company> getAllByStatus(CommonQuery query) {
		return dao.getAllByStatus(query);
	}

	public String saveContact(Contact contact) {
		dao.saveContact(contact);
		return contact.getContactId();
	}

	public List<Contact> getContactList(String companyId, String contactId) {
		return dao.getContactList(companyId, contactId);
	}
}
